package com.liao.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liao.common.constant.order.WareLockStatusEnum;
import com.liao.common.entity.order.OrderItemEntity;
import com.liao.common.entity.product.SkuEntity;
import com.liao.common.entity.product.WareTaskEntity;
import com.liao.common.to.mq.StockLockedTo;
import com.liao.common.utils.PageUtils;
import com.liao.common.utils.Query;
import com.liao.product.dao.SkuDao;
import com.liao.product.service.SkuService;
import com.liao.product.service.WareTaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("skuService")
public class SkuServiceImpl extends ServiceImpl<SkuDao, SkuEntity> implements SkuService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    WareTaskService wareTaskService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuEntity> page = this.page(
                new Query<SkuEntity>().getPage(params),
                new QueryWrapper<SkuEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void lockStock(List<OrderItemEntity> orderItemEntities) {

        List<WareTaskEntity> wareTaskEntityList = new ArrayList<>();

        orderItemEntities.forEach(item -> {
//            锁定库存
            lock(Integer.valueOf(item.getSkuId()), item.getQuantity());


            WareTaskEntity wareTask = new WareTaskEntity();
            wareTask.setLockStatus(WareLockStatusEnum.LOCKED);
            wareTask.setSkuId(item.getSkuId());
            wareTask.setOrderId(item.getOrderId());
            wareTask.setSkuNum(item.getQuantity());

            wareTaskEntityList.add(wareTask);
        });


        // 7.发送消息
        for (WareTaskEntity taskDetail : wareTaskEntityList) {
            wareTaskService.save(taskDetail);
            System.out.println("保存工作单数据" + taskDetail);
            StockLockedTo stockLockedTo = new StockLockedTo();
            BeanUtils.copyProperties(taskDetail, stockLockedTo);
            stockLockedTo.setOrderId(taskDetail.getOrderId());
            rabbitTemplate.convertAndSend("stock-event-exchange", "stock.locked", stockLockedTo);
        }


    }

    @Override
    public void unlockStock(Integer skuId, Integer num) {
        QueryWrapper<SkuEntity> skuEntityQueryWrapper = new QueryWrapper<>();
        skuEntityQueryWrapper.eq("sku_id", skuId);
        SkuEntity one = this.getOne(skuEntityQueryWrapper);
        one.setStock(one.getStock() + num);
        one.setLockStock(one.getLockStock() - num);
        this.updateById(one);
    }


    public void lock(Integer skuId, Integer count) {
        QueryWrapper<SkuEntity> skuEntityQueryWrapper = new QueryWrapper<>();
        skuEntityQueryWrapper.eq("sku_id", skuId);
        SkuEntity one = this.getOne(skuEntityQueryWrapper);

        if (one == null) {
            throw new RuntimeException(skuId + ":商品不存在");
        }
        if (one.getStock() < count) {
            throw new RuntimeException(skuId + ":库存不足");
        } else {
            one.setStock(one.getStock() - count);
            one.setLockStock(one.getLockStock() + count);
            this.updateById(one);
        }
    }

}